/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.ux.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.softwareinlife.vision.android.data.dao.PlanDAO;
import com.softwareinlife.vision.android.data.dao.UserDAO;
import com.softwareinlife.vision.android.data.dao.VisionDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.model.PlanModel;
import com.softwareinlife.vision.android.data.model.UserModel;
import com.softwareinlife.vision.android.data.model.VisionModel;
import com.softwareinlife.vision.android.ux.UXConstants;

/**
 * Shared Preference Utility
 * 
 * @author Jang, Sun-Jin (jangsunjin@softwareinlife.com)
 * @date 2009. 10. 11.
 */
public class SharedPreferenceUtility {

	/**
	 * Get Shared Preferences
	 * 
	 * @param ctx
	 * @return SharedPreferences
	 */
	public static SharedPreferences getSharedPreferences(Context ctx) {
		return ctx.getSharedPreferences(UXConstants.SHARED_PREFERENCE,
				UXConstants.SHARED_PREFERENCE_MODE);
	}

	/**
	 * Get User ID
	 * 
	 * @param ctx
	 * @return User ID
	 */
	public static String getUserId(Context ctx) {
		return getSharedPreferences(ctx).getString(UXConstants.USER_ID, null);
	}
	
	/**
	 * Set User ID
	 * 
	 * @param ctx
	 * @return User ID
	 */
	public static void setUserId(Context ctx, String userId) {
		SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
		editor.putString(UXConstants.USER_ID, userId);
		editor.commit();
	}

	/**
	 * Get User Model
	 * 
	 * @param applicationContext
	 * @return UserModel
	 */
	public static UserModel getUserModel(Context ctx) {
		String userId = getUserId(ctx);
		
		if (userId != null && userId.length() > 0) {
			UserDAO dao = null;
			try {
				dao = new UserDAO(ctx);
				return dao.read(userId);
			} catch (DatabaseConnectionException e) {
				e.printStackTrace();
			}
			
			if (dao != null) {
				dao.close();	
			}
		}
		
		return null;
	}

	/**
	 * Set Vision Id
	 * 
	 * @param ctx
	 * @param visionId
	 */
	public static void setVisionId(Context ctx, int visionId) {
		SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
		editor.putInt(UXConstants.VISION_ID, visionId);
		editor.commit();
	}
	

	/**
	 * Get Vision Model
	 * 
	 * @param applicationContext
	 * @return VisionModel
	 */
	public static VisionModel getVisionModel(Context ctx) {
		int visionIds = getVisionId(ctx);
		
		if (visionIds > 0) {
			VisionDAO dao = null;
			try {
				dao = new VisionDAO(ctx);
				return dao.read(visionIds);
			} catch (DatabaseConnectionException e) {
				e.printStackTrace();
			}
			
			if (dao != null) {
				dao.close();	
			}
		}
		
		return null;
	}
	
	/**
	 * Get Vision ID
	 * 
	 * @param ctx
	 * @return User ID
	 */
	public static int getVisionId(Context ctx) {
		return getSharedPreferences(ctx).getInt(UXConstants.VISION_ID, 0);
	}
	
	/**
	 * Set Plan Id
	 * 
	 * @param ctx
	 * @param visionId
	 */
	public static void setPlanId(Context ctx, int planId) {
		SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
		editor.putInt(UXConstants.PLAN_ID, planId);
		editor.commit();
	}
	
	/**
	 * Get Plan ID
	 * 
	 * @param ctx
	 * @return User ID
	 */
	public static int getPlanId(Context ctx) {
		return getSharedPreferences(ctx).getInt(UXConstants.PLAN_ID, 0);
	}
	
	/**
	 * Set Schedule Id
	 * 
	 * @param ctx
	 * @param scheduleId
	 */
	public static void setScheduleId(Context ctx, int scheduleId) {
		SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
		editor.putInt(UXConstants.SCHEDULE_ID, scheduleId);
		editor.commit();
	}
	
	/**
	 * Get Schedule ID
	 * 
	 * @param ctx
	 * @return User ID
	 */
	public static int getScheduleId(Context ctx) {
		return getSharedPreferences(ctx).getInt(UXConstants.SCHEDULE_ID, 0);
	}

	/**
	 * Get Plan Model
	 * 
	 * @param application
	 * @return
	 */
	public static PlanModel getPlanModel(Context ctx) {
		int planID = getPlanId(ctx);
		
		if (planID > 0) {
			PlanDAO dao = null;
			try {
				dao = new PlanDAO(ctx);
				return dao.read(planID);
			} catch (DatabaseConnectionException e) {
				e.printStackTrace();
			}
			
			if (dao != null) {
				dao.close();	
			}
		}
		
		return null;
	}
}
